package com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.time.LocalDateTime;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.TranslateUtil;
import com.xjrsoft.core.tool.utils.aiplm.AiplmTxt2imgParam;
import com.xjrsoft.core.tool.utils.aiplm.AiplmTxt2imgResult;
import com.xjrsoft.core.tool.utils.aiplm.AiplmUtil;
import com.xjrsoft.module.base.entity.XjrBaseAnnexesFile;
import com.xjrsoft.module.base.service.IXjrBaseAnnexesFileService;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_design.dto.TAiplmDesignDto;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_design.entity.TAiplmDesign;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_design.service.ITAiplmDesignService;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_model.entity.TAiplmModel;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_model.service.ITAiplmModelService;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.dto.TAiplmText2imgSimpleDto;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.dto.SaveTAiplmText2imgSimpleFormDataDto;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.vo.TAiplmText2imgSimpleVo;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.vo.TAiplmText2imgSimpleListVo;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.dto.TAiplmText2imgSimpleListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.entity.TAiplmText2imgSimple;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.vo.TAiplmText2imgSimpleVo;
import com.xjrsoft.module.customerTwo.aiplm.t_aiplm_text2img_simple.service.ITAiplmText2imgSimpleService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-07-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_aiplm_text2img_simple")
@Api(value = "智能微设计", tags = "智能微设计接口")
public class TAiplmText2imgSimpleController {
	private final ITAiplmText2imgSimpleService tAiplmText2imgSimpleService;
	@Autowired
	private IXjrBaseAnnexesFileService annexesFileService;
	@Autowired
	private ITAiplmModelService aiplmModelService;
	@Autowired
	private ITAiplmDesignService tAiplmDesignService;


	/**
	 * 生成图片
	 */
	@PostMapping("/createImg")
	@ApiOperation(value = "生成图片", notes = "传入tAiplmText2imgSimple")
	public Response createImg(@RequestBody TAiplmText2imgSimpleDto formDto) {
		String[] response = new String[4];
		TAiplmModel tAiplmModel = aiplmModelService.getById(formDto.getModleId());
		if(null != tAiplmModel){
			//组装参数
			AiplmTxt2imgParam param = new AiplmTxt2imgParam(tAiplmModel.getFSdmodel());
			param.setBatch_size(formDto.getBatchSize());
			param.setCfg_scale(tAiplmModel.getFCfgscale());
			param.setNegative_prompt(tAiplmModel.getFNegativeprompt());
			if(StringUtils.isNotBlank(formDto.getPrompt())){
				String prompt = formDto.getPrompt().replace("，",",");
				param.setPrompt(tAiplmModel.getFPrompt()+","+ TranslateUtil.aliGeneralTranslate(prompt));
			}else {
				param.setPrompt(tAiplmModel.getFPrompt());
			}
			param.setSampler_index(tAiplmModel.getFSamplerindex());
			param.setSeed(tAiplmModel.getFSeed());
			param.setSteps(tAiplmModel.getFSteps());
			param.setWidth(formDto.getP_width());
			param.setHeight(formDto.getP_height());
//			if(formDto.getResolvingPower() == 1){
//				param.setEnable_hr(true);
//			}
			//文生图
			AiplmTxt2imgResult result = AiplmUtil.txt2img(param);
			if(null == result){
				return Response.notOk("文生图失败，请联系管理员！");
			}
			if(StringUtils.isNotBlank(result.getErrorMsg())){
				return Response.notOk("文生图异常，请检查  "+result.getErrorMsg());
			}
			if(CollectionUtil.isNotEmpty(result.getImages())){
				//base64转图片
				int i=0;
				for(String base64Str:result.getImages()){
					String id = IdWorker.get32UUID();
					XjrBaseAnnexesFile file = AiplmUtil.convertBase64ToImage(base64Str,id+".png");
					file.setId(id);
					file.setFolderId(id);
					annexesFileService.save(file);
					response[i] = id;
					i++;
				}
			}
		}else {
			return Response.notOk("产品类型["+formDto.getModleId()+"]不存在，请检查");
		}
		return Response.ok(response);
	}

	@PostMapping("/saveMyDesign")
	@ApiOperation(value = "保存到我的设计", notes = "传入tAiplmText2imgSimple")
	public Response saveMyDesign(@RequestBody TAiplmDesignDto formDto) {
		TAiplmDesign tAiplmDesign = new TAiplmDesign();
		tAiplmDesign.setFId(IdWorker.get32UUID());
		tAiplmDesign.setFKeywork(formDto.getFKeywork());
		tAiplmDesign.setFPicture(formDto.getFPicture());
		tAiplmDesign.setFCreateuserid(SecureUtil.getUserId());
		tAiplmDesign.setFCreatedate(LocalDateTime.now());
		tAiplmDesignService.save(tAiplmDesign);
		return Response.ok();
	}

}
